const conn = require('../app/database')

class AddressModel {
	async getAddressListByUid(uid) {
		const statement = `
      SELECT 
        a.id addressId, a.user_id userId, a.user_name userName, a.tel_number telNumber, a.region region, 
        a.detail_info detailInfo
      FROM address a
      WHERE user_id = ?;
    `
		const [result] = await conn.execute(statement, [uid])

		return result
	}

  async getAddressByid(addressId) {
    const statement = `
      SELECT 
        a.id addressId, a.user_id userId, a.user_name userName, a.tel_number telNumber, a.region region, 
        a.detail_info detailInfo
      FROM address a
      WHERE id = ?;
    `
		const [result] = await conn.execute(statement, [addressId])

		return result
  }

	async getAddressInfo(userId, userName, telNumber, region, detailInfo) {
		const statement = `
      SELECT 
				a.id addressId, a.user_id userId, a.user_name userName, a.tel_number telNumber, a.region region, 
				a.detail_info detailInfo
			FROM address a
      WHERE user_id=? AND user_name=? AND tel_number=? AND region=? AND detail_info=?;
    `
		const [result] = await conn.execute(statement, [userId, userName, telNumber, region, detailInfo])

		return result
	}

	async createAddress(
		userId,
		userName,
		telNumber,
		region,
		detailInfo
	) {
		const statement = `
      INSERT INTO address (user_id, user_name, tel_number, region, detail_info) VALUES (?,?,?,?,?);
    `
		const [result] = await conn.execute(statement, [
			userId,
			userName,
			telNumber,
			region,
			detailInfo
		])

		return result
	}

	async updateAddressById(
		userName,
		telNumber,
		region,
		detailInfo,
		addressId
	) {
		const statement = `
      UPDATE address 
      SET user_name=?, tel_number=?, region=?, detail_info=?
      WHERE id=?;
    `
		const [result] = await conn.execute(statement, [
			userName,
			telNumber,
			region,
			detailInfo,
			addressId,
		])

		return result
	}

	async deleteAddressById(addressId) {
		const statement = `
      DELETE FROM address WHERE id=?;
    `
		const [result] = await conn.execute(statement, [addressId])

		return result
	}
}

module.exports = new AddressModel()
